﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace Obfuscator
{
    public partial class ObfuscateAssemblies : Form
    {
        public ObfuscateAssemblies()
        {
            InitializeComponent();
            if (MainForm.tvAssembliesMain.Nodes.Count != 0)
            {
                //tvAssemblies.= MainForm.tvAssembliesMain.Nodes;
            }
            AssembliesNames = new List<string>();
        }
        public TreeView tvAssembliesView = new TreeView();
        public List<string> AssembliesNames;
        #region Add Assembly

        private void tsbAddAssembly_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = Application.StartupPath;
            dialog.Multiselect = true;
            DialogResult result = dialog.ShowDialog(this);

            if (result != DialogResult.OK)
                return;

            foreach (string file in dialog.FileNames)
            {
                AddAssembly(file);
                AssembliesNames.Add(file);
            }

            //--- Epand
            tvAssemblies.Nodes[0].Expand();
        }

        private void AddAssembly(string file)
        {
            // Assembly
            Assembly assembly = Assembly.LoadFrom(file);
            TreeNode assemblyNode = tvAssemblies.Nodes[0].Nodes.Add(file);
            assemblyNode.Tag = assembly;
            assemblyNode.Checked = true;
            assemblyNode.ImageIndex = assemblyNode.SelectedImageIndex = 1;

            try
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.FullName.StartsWith("<PrivateImplementationDetails>"))
                        continue;

                    if (type.FullName.Contains("+") && type.FullName.IndexOf("__", type.FullName.IndexOf("+")) > -1)
                        continue;

                    TreeNode typeNode = assemblyNode.Nodes.Add(type.FullName);
                    typeNode.Tag = type;
                    typeNode.Checked = true;

                    if (type.IsEnum)
                        typeNode.ImageIndex = typeNode.SelectedImageIndex = 4;
                    else
                        typeNode.ImageIndex = typeNode.SelectedImageIndex = 3;
                }
            }
            catch (ReflectionTypeLoadException exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        #endregion

        #region Remove Assembly

        private void tsbRemoveAssembly_Click(object sender, EventArgs e)
        {
            tvAssemblies.SelectedNode.Parent.Nodes.Remove(tvAssemblies.SelectedNode);
        }

        #endregion

        private void btnOkObfuscateAss_Click(object sender, EventArgs e)
        {
            tvAssembliesView = tvAssemblies;
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancelObfuscateAss_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
